import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Queue;

public class MyStack {
    Deque<Integer> queue1, queue2;
    public MyStack() {
        queue1 = new ArrayDeque<>();
        queue2 = new ArrayDeque<>();
    }

    public void push(int x) {
        queue1.offer(x);
    }

    public int pop() {
        while (queue1.size() > 1) {
            queue2.offer(queue1.poll());
        }
        int ret = queue1.poll();
        while (!queue2.isEmpty()) {
            queue1.offer(queue2.poll());
        }
        return ret;
    }

    public int top() {
        while (queue1.size() > 1) {
            queue2.offer(queue1.poll());
        }
        int ret = queue1.peek();
        queue2.offer(queue1.poll());
        while (!queue2.isEmpty()) {
            queue1.offer(queue2.poll());
        }
        return ret;
    }

    public boolean empty() {
        return queue1.isEmpty();
    }
}
